import { getStorage, removeStorage, setStorage } from './localstoreage';
import { getSession } from './sessionStorage';
import { logout } from './toolkit';

const TokenKey = 'LHToken';
export function getToken() {
  return getStorage(TokenKey);
}
export function setToken(token: string) {
  setStorage(TokenKey, token);
}
export function removeToken() {
  removeStorage(TokenKey);
}

const UserIdKey = 'userId';
export function getUserId(): string | null {
  let userId = getStorage(UserIdKey);
  if (!userId) {
    logout()
    return null
  }
  return getStorage(UserIdKey);
}
export function setUserId(userId: string) {
  setStorage(UserIdKey, userId);
}
export function removeUserId() {
  removeStorage(UserIdKey);
}

const UserRolesKey = 'userRoles';
export function getUserRoles(): number[] | null {
  return Array.isArray(getStorage(UserRolesKey)) ? getStorage(UserRolesKey) : null;
}
export function setUserRoles(userStatus: number[]) {
  setStorage(UserRolesKey, userStatus);
}
export function removeUserRoles() {
  removeStorage(UserRolesKey);
}

const UserNameKey = 'userName';
export function getUserName(): string | null {
  return getStorage(UserNameKey);
}
export function setUserName(userName: string) {
  // return Cookies.set(UserNameKey, userName, cookiesOption);
  setStorage(UserNameKey, userName);
}
export function removeUserName() {
  removeStorage(UserNameKey);
}

const UserPassKey = 'userPass';
export function getUserPass(): string | null {
  return getStorage(UserPassKey);
}
export function setUserPass(userPass: string) {
  setStorage(UserPassKey, userPass);
}
export function removeUserPass() {
  removeStorage(UserPassKey);
}

export type UserStatus = { login: boolean }
const UserStatusKey = 'userStatus';
export function getUserStatus(): UserStatus {
  const userStatus = getStorage(UserStatusKey)
  return userStatus === null ? { login: false } : userStatus;
}
export function setUserStatus(stastus: UserStatus) {
  setStorage(UserStatusKey, stastus);
}
export function removeUserStatus() {
  removeStorage(UserStatusKey);
}
